package ui;

import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.RenderingHints.Key;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private Image image;
	private Dimension screen = new Dimension(java.awt.Toolkit
			.getDefaultToolkit().getScreenSize().width, java.awt.Toolkit
			.getDefaultToolkit().getScreenSize().height);
	private Image image2;

	public ImagePanel(String file) {
		this(new ImageIcon(file).getImage());
	}
	public ImagePanel(String string, String string2) {
		this(new ImageIcon(string).getImage(),new ImageIcon(string2).getImage());
	}

	public ImagePanel() {

	}

	public ImagePanel(Image image) {
		this.setImage(image);
		configPanel();
	}


	public ImagePanel(Image image, Image image2) {
		this.setImage(image);
		this.setImage2(image2);
		configPanel();
	}
	public void setImage2(Image image2) {
		this.image2=image2.getWidth(null)>133? resize(toBufferedImage(image2,false),133,100): image2;
	}
	
	
	protected void configPanel() {
		int width = getImage().getWidth(null) > screen.width ? screen.width
				: getImage().getWidth(null);
		setMaximumSize(new Dimension(width, getImage().getHeight(null)));
		setSize(new Dimension(width, getImage().getHeight(null)));
		setPreferredSize(new Dimension(width, getImage().getHeight(null)));
		setMinimumSize(new Dimension(width, getImage().getHeight(null)));
	}

	@Override
	public void paintComponent(Graphics g) {
		g.drawImage(getImage(), 0, 0, null);
		if(getImage2()!=null) g.drawImage(getImage2(), getPreferredSize().width-getImage2().getWidth(null)-5, 0, null);
	}

	private Image getImage2() {		
		return this.image2;
	}
	@Override
	public void setEnabled(boolean e) {
		this.setVisible(e);
		this.revalidate();
		this.repaint();
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}
	
	protected BufferedImage resize(BufferedImage image, int width, int height) {
		HashMap<Key, Object> map = new HashMap<Key, Object>();
		map.put(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);

		map.put(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);

		map.put(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		RenderingHints hints = new RenderingHints(map);
		int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image
				.getType();
		BufferedImage resizedImage = new BufferedImage(width, height, type);
		Graphics2D g = resizedImage.createGraphics();
		g.setComposite(AlphaComposite.Src);
		g.addRenderingHints(hints);

		g.drawImage(image, 0, 0, width, height, null);
		g.dispose();
		return resizedImage;
	}
	
	protected BufferedImage toBufferedImage(Image image, boolean hasAlpha) {
		if (image instanceof BufferedImage) {
			return (BufferedImage) image;
		}
		image = new ImageIcon(image).getImage();
		BufferedImage bimage = null;
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		try {
			int transparency = hasAlpha ? Transparency.BITMASK
					: Transparency.OPAQUE;
			GraphicsDevice gs = ge.getDefaultScreenDevice();
			GraphicsConfiguration gc = gs.getDefaultConfiguration();
			bimage = gc.createCompatibleImage(image.getWidth(null),
					image.getHeight(null), transparency);
		} catch (HeadlessException e) {
		}

		if (bimage == null) {

			int type = BufferedImage.TYPE_INT_RGB;
			if (hasAlpha) {
				type = BufferedImage.TYPE_INT_ARGB;
			}
			bimage = new BufferedImage(image.getWidth(null),
					image.getHeight(null), type);
		}

		Graphics g = bimage.getGraphics();

		// Paint the image onto the buffered image
		g.drawImage(image, 0, 0, null);
		g.dispose();

		return bimage;
	}
	
	

}
